package com.freemalll.system.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.freemalll.common.core.constant.UserConstants;
import com.freemalll.common.core.utils.StringUtils;
import com.freemalll.system.domain.SysApplication;
import com.freemalll.system.mapper.SysApplicationMapper;
import com.freemalll.system.service.ISysApplicationService;

/**
 * 系统应用 服务层实现
 * 
 * @author freemalll
 */
@Service
public class SysApplicationServiceImpl implements ISysApplicationService
{
    @Autowired
    private SysApplicationMapper applicationMapper;

    /**
     * 查询系统应用信息
     * 
     * @param id 应用ID
     * @return 系统应用信息
     */
    @Override
    public SysApplication selectApplicationById(String id)
    {
        return applicationMapper.selectApplicationById(id);
    }

    /**
     * 根据应用编码查询应用信息
     * 
     * @param appCode 应用编码
     * @return 系统应用信息
     */
    @Override
    public SysApplication selectApplicationByAppCode(String appCode)
    {
        return applicationMapper.selectApplicationByAppCode(appCode);
    }

    /**
     * 查询系统应用列表
     * 
     * @param application 系统应用信息
     * @return 系统应用集合
     */
    @Override
    public List<SysApplication> selectApplicationList(SysApplication application)
    {
        return applicationMapper.selectApplicationList(application);
    }

    /**
     * 新增系统应用
     * 
     * @param application 系统应用信息
     * @return 结果
     */
    @Override
    public int insertApplication(SysApplication application)
    {
        return applicationMapper.insert(application);
    }

    /**
     * 修改系统应用
     * 
     * @param application 系统应用信息
     * @return 结果
     */
    @Override
    public int updateApplication(SysApplication application)
    {
        return applicationMapper.updateById(application);
    }

    /**
     * 批量删除系统应用
     * 
     * @param ids 需要删除的应用ID
     */
    @Override
    public void deleteApplicationByIds(String[] ids)
    {
        for (String id : ids)
        {
            applicationMapper.deleteById(id);
        }
    }

    /**
     * 删除系统应用信息
     * 
     * @param id 应用ID
     * @return 结果
     */
    @Override
    public int deleteApplicationById(String id)
    {
        return applicationMapper.deleteById(id);
    }

    /**
     * 校验应用编码是否唯一
     * 
     * @param application 应用信息
     * @return 结果
     */
    @Override
    public boolean checkAppCodeUnique(SysApplication application)
    {
        String id = StringUtils.isNull(application.getId()) ? "-1" : application.getId();
        SysApplication info = applicationMapper.selectApplicationByAppCode(application.getAppCode());
        if (StringUtils.isNotNull(info) && !info.getId().equals(id))
        {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    /**
     * 校验应用名称是否唯一
     * 
     * @param application 应用信息
     * @return 结果
     */
    @Override
    public boolean checkAppNameUnique(SysApplication application)
    {
        String id = StringUtils.isNull(application.getId()) ? "-1" : application.getId();
        SysApplication info = applicationMapper.selectApplicationByAppName(application.getAppName());
        if (StringUtils.isNotNull(info) && !info.getId().equals(id))
        {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }
}